﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataSystem
{
    public class SQLParser
    {
        public enum SQLAction
        {
            SELECT,
            UPDATE,
            INSERT,
            DELETE
        }

        public class WhereOperator
        {
            public string Column { get; set; }

            public string Param { get; set; }

            public string Op { get; set; }
        }

        public SQLAction Action { get; set; }

        public List<string> Columns { get; set; }

        public string ObjectName { get; set; }

        public List<string> CommandText = new List<string>();

        public List<WhereOperator> WhereOperators { get; set; }
        public bool Valid { get; set; }

        public SQLParser(string sql)
        {
            try
            {
                Valid = true;
                CommandText.AddRange(sql.ToLower().Split(' '));
                // get action from first string
                WhereOperators = new List<WhereOperator>();
                Columns = new List<string>();

                switch (CommandText.First())
                {
                    case "select":
                        Action = SQLAction.SELECT;
                        break;
                    case "update":
                        Action = SQLAction.UPDATE;
                        break;
                    case "insert":
                        Action = SQLAction.INSERT;
                        break;
                    case "delete":
                        Action = SQLAction.DELETE;
                        break;
                    default:
                        break;
                }

                CommandText.RemoveAt(0);

                if (Action == SQLAction.SELECT)
                {
                    while (CommandText.First().ToLower() != "from")
                    {
                        Columns.Add(CommandText.First());
                        CommandText.Remove(CommandText.First());
                    }
                    if (CommandText.First().ToLower() == "from") CommandText.Remove(CommandText.First());
                    // this is the data object
                    this.ObjectName = CommandText.First();
                    CommandText.Remove(CommandText.First());
                    // where 
                    if (this.CommandText.First() == "where")
                    {
                        CommandText.Remove(CommandText.First());
                        while (CommandText.Count != 0)
                        {
                            // TODO Fix this crap
                            if (CommandText.First() == "and" || CommandText.First() == "or")
                                CommandText.Remove(CommandText.First());
                            else
                            {
                                // get 3
                                WhereOperator wo = new WhereOperator() { Column = CommandText[0], Op = CommandText[1], Param = CommandText[2] };
                                // delete 3
                                CommandText.Remove(CommandText.First());
                                CommandText.Remove(CommandText.First());
                                CommandText.Remove(CommandText.First());
                                WhereOperators.Add(wo);
                            }



                        }



                    }
                    else return;
                }
            }
            catch
            {
                Valid = false;
            }
        }

        public string GetWhereParamByColumn(string col)
        {
            foreach (var p in WhereOperators)
            {
                if (p.Column == col) return p.Param;
            }
            return null;
        }



    }





}

